package com.abiao.shop.service;

import com.abiao.shop.dao.GoodsDao;
import com.abiao.shop.exception.GlobalException;
import com.abiao.shop.pojo.DiscountGoods;
import com.abiao.shop.pojo.Goods;
import com.abiao.shop.redis.RedisService;
import com.abiao.shop.result.CodeMsg;
import com.abiao.shop.vojo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: GoodsService   商品业务操作类
 * @Author: AB
 * @Date: 2021-07-16
 * @Version:v1.0
 */
@Service
public class GoodsService {

    public final static String GOODS_LIST_KEY = "goodsList";

    @Autowired
    GoodsDao goodsDao;
    @Autowired
    RedisService redisService;

    /**
     * 获取所有普通商品
     * @return
     */
    public List<Goods> getALLGoods(){
        return goodsDao.getAllGoods();
    }

    /**
     * 获取所有商品（秒杀商品和普通商品）
     * @return
     */
    public List<GoodsVo> goodsVoList(){
        return goodsDao.goodsVoList();
    }

    /**
     * 根据id获取商品
     * @param goodsId
     * @return
     */
    public GoodsVo getGoodsVoById(long goodsId) {
        GoodsVo goodsVo = goodsDao.getGoodsVoById(goodsId);
        if (goodsVo == null){
            throw new GlobalException(CodeMsg.SERVER_ERROR);
        }
        return goodsVo;
    }

    /**
     * 商品库存减一
     * @param goodsVo
     */
    public boolean reduceStock(GoodsVo goodsVo) {
        DiscountGoods g = new DiscountGoods();
        g.setGoodsId(goodsVo.getId());
        int count = goodsDao.reduceStock(g);
        return count>0;
    }
}
